/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define HUDMODULE
#include "HUDScrollbar.h"

#include <iostream>

namespace ModularEngine
{

	HUDScrollbar::HUDScrollbar()
	{
		mContentHeight = 0;
		mScroll = 0;

		LOADCOLORS;

	}

	HUDScrollbar::~HUDScrollbar()
	{

	}

	int HUDScrollbar::initialize()
	{
		return 1;
	}

	void HUDScrollbar::draw()
	{

#define UPHEIGHT 17
#define SPEED 10
		
		setScroll( mScroll );

		// Mouse Functions:
		if( mbUp )
		{
			if( !isMouseDown() )
			{
				mbUp = false;
			} else
			{
				mScroll -= SPEED;
			}

			if( mScroll < 0 )
				mScroll = 0;
		}

		if( mbDown )
		{
			if( !isMouseDown() )
			{
				mbDown = false;
			} else
			{
				mScroll += SPEED;
			}

			if( mScroll > mContentHeight - mvDimensions.y )
				mScroll = mContentHeight - mvDimensions.y;
		}

		if( mbMouseDown )
		{
			if( !isMouseDown() )
			{
				mbMouseDown = false;
			} else
			{		

				int mouseOffset = mMouseInitial.y - WindowModule::getSingleton().getMouse().y;
				mMouseInitial = WindowModule::getSingleton().getMouse();

				float scrollPercent = mouseOffset/(mvDimensions.y-UPHEIGHT*2-mScrollHeight);
				mScroll -= scrollPercent * (mContentHeight-mvDimensions.y);

				if( mScroll < 0 )
					mScroll = 0;
				if( mScroll > mContentHeight-mvDimensions.y )
					mScroll = mContentHeight-mvDimensions.y;


			}
		}

		gl->setCoords( mvPosition );

		glColor3f( mvBaseColor.x+.2, mvBaseColor.y+.2, mvBaseColor.z+.2 );
		gl->drawGradientRect( Vector2d( 0, 0 ), mvDimensions, mvBaseColor-.3, mvBaseColor-.1, true );

		glColor3f( .6, .6, .6 );
		gl->drawGradientRect( Vector2d( 0, 0 ), Vector2d( mvDimensions.x, UPHEIGHT ), mvBaseColor, mvBaseColor+.3, true );
		glColor4f( 1, 1, 1, 1 );
		gl->drawImage( "uparrow.tga", Vector2d( 0, 0 ) );
		glColor3f( .6, .6, .6 );
		gl->drawGradientRect( Vector2d( 0, mvDimensions.y - UPHEIGHT ), Vector2d( mvDimensions.x, UPHEIGHT ),mvBaseColor, mvBaseColor+.3, true );
		glColor4f( 1, 1, 1, 1 );
		gl->drawImage( "downarrow.tga", Vector2d( 0, mvDimensions.y-UPHEIGHT ) );

		if( mContentHeight > mvDimensions.y )
		{
			glColor3f( .5, .5, .5 );
			float scrollPercent = mScroll/(mContentHeight-mvDimensions.y);
			int yPos = scrollPercent * (mvDimensions.y - UPHEIGHT * 2 - mScrollHeight);
			glColor4f( 0, 0, 0, .5 );
			gl->drawRect( Vector2d( 1, UPHEIGHT + yPos-1 ), Vector2d( mvDimensions.x-1, mScrollHeight+2 ) );
			gl->drawGradientRect( Vector2d( 0, UPHEIGHT + yPos ), Vector2d( mvDimensions.x, mScrollHeight ), mvBaseColor-.3, mvBaseColor-.6, true );
		} else 
		{
			glColor4f( 0, 0, 0, .5 );
			gl->drawRect( Vector2d( -1, UPHEIGHT-1 ), Vector2d( mvDimensions.x+2, mvDimensions.y-UPHEIGHT*2+2 ) );
			gl->drawGradientRect( Vector2d( 0, UPHEIGHT ), Vector2d( mvDimensions.x, mvDimensions.y-UPHEIGHT*2 ), mvBaseColor-.3, mvBaseColor, true );
		}
	}

	void HUDScrollbar::setContentHeight( int nHeight )
	{
		mContentHeight = nHeight;
		mScrollHeight = mvDimensions.y*(mvDimensions.y-UPHEIGHT)/mContentHeight;
		if( mScrollHeight < UPHEIGHT )
			mScrollHeight = UPHEIGHT;
		if( mScrollHeight > mvDimensions.y-UPHEIGHT*2)
			mScrollHeight = mvDimensions.y-UPHEIGHT*2;

	}

	int HUDScrollbar::getScroll()
	{
		return mScroll;
	}

	void HUDScrollbar::setScroll( int nValue )
	{
		mScroll = nValue;
		if( mScroll < 0 )
			mScroll = 0;
		if( mScroll > mContentHeight-mvDimensions.y )
			mScroll = mContentHeight-mvDimensions.y;
	}

	void HUDScrollbar::mouseInput( Vector2d vPos, UINT nDown )
	{

		if( vPos.y < mvPosition.y+UPHEIGHT )
		{
			mbUp = true;
		}

		if( vPos.y > mvPosition.y+mvDimensions.y-UPHEIGHT )
		{
			mbDown = true;
		}

		if( vPos.y > mvPosition.y+UPHEIGHT && vPos.y < mvPosition.y+mvDimensions.y-UPHEIGHT )
		{	
			mbMouseDown = true;
			mMouseInitial = vPos;
		}

		

	}

	void HUDScrollbar::keyboardInput( UINT nKey ) 
	{

	}

}
